﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Factories;
using TimeSplicerModule.Services;
using TimeSplicerModule.ViewModels;


namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for ProjectRoleView.xaml
    /// </summary>
    public partial class ProjectRoleView : UserControl, INavigationAware
    {
        private ProjectRoleViewModel Model;
        private readonly IUnityContainer _container;
        private readonly IProjectRoleService _projectRoleService;
        private readonly IProjectRoleController _projectRoleController;

        public ProjectRoleView(IUnityContainer container, IProjectRoleService projectRoleService, IProjectRoleController projectRoleController)
        {
            InitializeComponent();

            _container = container;
            _projectRoleService = projectRoleService;
            _projectRoleController = projectRoleController;
        }



        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.Count() == 0)
            {
                await Task.Run(() => Model = _projectRoleService.CreateNew());
                //Model = _projectRoleFactory.CreateNew();
            }
            else
            {
                string id = navigationContext.Parameters["id"].ToString();
                Guid result;

                if (Guid.TryParse(id, out result))
                {
                    await Task.Run(() => Model = _projectRoleService.Get(result));
                    //Model = _projectRoleFactory.Get(x => x.ProjectRoleGuid == result);
                }
            }


            this.DataContext = Model;
        }

        private async void saveButton_Click(object sender, RoutedEventArgs e)
        {
            //Model = _projectRoleFactory.Upsert(Model);
            this.saveButton.IsEnabled = false;
            this.saveExitButton.IsEnabled = false;
            this.cancelButton.IsEnabled = false;

            await Task.Run(() => Model = _projectRoleService.Save(Model));

            this.saveButton.IsEnabled = true;
            this.saveExitButton.IsEnabled = true;
            this.cancelButton.IsEnabled = true;
        }

        private void saveExitButton_Click(object sender, RoutedEventArgs e)
        {
            saveButton_Click(sender, e);
            cancelButton_Click(sender, e);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            _projectRoleController.NavigateToListView();
        }
    }
}
